//导入共享数据包
import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import { store } from '../../store/store'
//引入vant提供的提示组件
import Toast from '@vant/weapp/toast/toast';

// pages/aticleContent/articleContent.js
Page({

    //共享数据相关配置
    options: {
        styleIsolation: 'shared'
    },

    behaviors: [storeBindingsBehavior],

    //引入需要的共享数据
    storeBindings: {
        store,
        fields: {
            article: 'articleDto',
            token: 'token'
        },
        actions: {
            updateArticleDto: 'updateArticleDto'
        }
    },

    /**
     * 页面的初始数据
     */
    data: {
        articleAuthor: String,
        commentList: [],
        isCommenting: false,
        inputMessage: null,

        //文章点赞数据
        isLiked: false,
        totalLikeNum: 0
    },

    //获取文章点赞数据方法
    articleGetLikeNum() {
        wx.request({
          url: `${store.programURL}/article/getArticleLikeNum/${store.articleDto.id}`,
          method: 'GET',
          success: (res) => {
              this.setData({
                  totalLikeNum: res.data.data
              })
          }
        })
    },

    //文章点赞与取消点赞方法
    articleLike() {
        //判断是否登录
        if(!store.token) {
            wx.showToast({
              title: '用户未登录',
              icon: 'error'
            })
            return
        }

        //点赞方法
        if(this.data.isLiked == false) {
            wx.request({
              url: `${store.programURL}/article/articleLike`,
              method: 'POST',
              header:{
                authentication: store.token
              },
              data: {
                userId: store.userInfo.id,
                articleId: store.articleDto.id
              },
              success: (res) => {
                  if(res.data.code != 1) {
                    wx.showToast({
                        title: '已点赞过',
                        icon: 'error'
                      })
                      this.setData({
                        isLiked: !this.data.isLiked
                    })
                      return
                  }
                this.articleGetLikeNum()
                this.setData({
                    isLiked: !this.data.isLiked
                })
              }
            })
            return
        }

        //取消点赞方法
        wx.request({
            url: `${store.programURL}/article/articleUndoLike`,
            method: 'POST',
            header:{
                authentication: store.token
            },
            data: {
              userId: store.userInfo.id,
              articleId: store.articleDto.id
            },
            success: () => {
              this.articleGetLikeNum()
              this.setData({
                  isLiked: !this.data.isLiked
              })
            }
          })
    },

    //提交评论方法
    submitComment() {
        if(!this.data.inputMessage) {
            wx.showToast({
              title: '留言不能为空',
              icon: 'error'
            })
            return
        }
        wx.request({
          url: `${store.programURL}/article/addComment`,
          method: 'POST',
          header:{
              authentication: store.token
          },
          data: {
              content: this.data.inputMessage,
              userId: store.userInfo.id,
              targetArticleId: store.articleDto.id,
              userAvatar: store.userInfo.avatarUrl,
              userName: store.userInfo.nickname
          },
          success: ()=> {
              wx.showToast({
                title: '留言成功',
              })
              //重置相关数据
              this.setData({
                  inputMessage: null,
                  isCommenting: false
              })
              this.loadComments()
          }
        })
    },

    //监听输入框内容修改
    onMessageChange(e) {
        this.setData({
            inputMessage: e.detail.value
        })
    },

    //正在写留言方法
    doComment() {
        if(!store.token) {
            wx.showToast({
              title: '用户未登录',
              icon: 'error'
            })
            return
        }
        this.setData({
            isCommenting: !this.data.isCommenting
        })
    },

    //加载评论列表方法
    loadComments() {
        wx.request({
          url: `${store.programURL}/article/loadComment/${store.articleDto.id}`,
          method: 'GET',
          success: (res) => {
            console.log(res)
            this.setData({
                commentList: res.data.data
            })
          }
        })
    },

    //添加好友方法
    addFriend() {
        if(!store.token) {
            wx.showToast({
              title: '用户未登录',
              icon: 'error'
            })
            return
        }
        wx.request({
          url: `${store.programURL}/chat/addFriend`,
          method: 'POST',
          header: {
            authentication: store.token
          },
          data: {
            userId: store.userInfo.id,
            friendId: store.articleDto.authorId,
            userName: store.userInfo.nickname,
            userFriendName: store.articleDto.authorName,
            userFriendAvatar: store.articleDto.authorAvatar
          },
          success: (res) => {
            if(res.data.code == 1) {
                Toast.success('已将TA添加至消息列表');
            } else {
                Toast.fail('TA已在您的消息列表中');
            }
          },
          fail: () => {
            Toast.fail('添加失败');
          }
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.loadComments()
        this.articleGetLikeNum()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})